// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */

#include "Import/BDFImport/BulkData/Element/CFASTParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"
#include "DataStructure/Input/Element/CFASTData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;

using namespace SG::DataStructure;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;
class CFASTParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (CFASTParserTest, ParseEntry1)
{
    std::vector<std::string> Entry = { "CFAST    3       1       ELEM    1       2       9" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseCFAST (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::CFASTData data;
    ASSERT_TRUE (DB_SERVICEFACTORY->get<IElementService> ()->FindById (3, data)
                 == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 3);
    ASSERT_TRUE (data.m_pId == 1);
    ASSERT_TRUE (data.m_ida == 1);
    ASSERT_TRUE (data.m_idb == 2);
    ASSERT_EQ (std::string (data.m_type), "ELEM");
    ASSERT_EQ (data.m_g[0] + 1, data.m_g[1]);
    ASSERT_EQ (data.m_gh[6] + 1, data.m_gh[7]);
    ASSERT_EQ (data.m_gs, 9);
}

TEST_F (CFASTParserTest, ParseEntry2)
{  //                                  $1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "CFAST   8050009780500097 ELEM    2512973 251194180520477 4017656 4017657" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseCFAST (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::CFASTData data;
    ASSERT_TRUE (DB_SERVICEFACTORY->get<IElementService> ()->FindById (80500097, data)
                 == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 80500097);
    ASSERT_TRUE (data.m_pId == 80500097);
    ASSERT_TRUE (data.m_ida == 2512973);
    ASSERT_TRUE (data.m_idb == 2511941);
    ASSERT_EQ (std::string (data.m_type), "ELEM");
    ASSERT_EQ (data.m_g[0], 4017656);
    ASSERT_EQ (data.m_g[1], 4017657);
    ASSERT_EQ (data.m_gh[6] + 1, data.m_gh[7]);
    ASSERT_EQ (data.m_gs, 80520477);
}

TEST_F (CFASTParserTest, ParseEntry3)
{  //                                     $1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567$1234567
    std::vector<std::string> Entry = { "CFAST       9719     163    PROP       8       9   10395   10395   10418ENTER   ",
                                       "ENTER         7.      8.      9." };
    //$1234567$1234567$1234567$1234567$1234567
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseCFAST (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::CFASTData data;
    ASSERT_TRUE (DB_SERVICEFACTORY->get<IElementService> ()->FindById (9719, data)
                 == SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_TRUE (data.m_id == 9719);
    ASSERT_TRUE (data.m_pId == 163);
    ASSERT_TRUE (data.m_xs == 7);
    ASSERT_TRUE (data.m_ys == 8);
    ASSERT_TRUE (data.m_zs == 9);
}